/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.web;

import com.jiadakeji.ppm.common.config.Global;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.dao.RoomDao;
import com.jiadakeji.ppm.modules.business.entity.AskRoom;
import com.jiadakeji.ppm.modules.business.entity.Room;
import com.jiadakeji.ppm.modules.business.service.AskRoomService;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 申请房屋Controller
 * @author HHAOYU
 * @version 2019-08-19
 */
@Controller
@RequestMapping(value = "${adminPath}/business/askRoom")
public class AskRoomController extends BaseController {

	@Autowired
	private AskRoomService askRoomService;
	@Autowired
	private RoomDao roomDao;
	
	@ModelAttribute
	public AskRoom get(@RequestParam(required=false) String id) {
		AskRoom entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = askRoomService.get(id);
		}
		if (entity == null){
			entity = new AskRoom();
		}
		return entity;
	}
	
	@RequiresPermissions("business:askRoom:view")
	@RequestMapping(value = {"list", ""})
	public String list(AskRoom askRoom, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AskRoom> page = askRoomService.findPage(new Page<AskRoom>(request, response), askRoom); 
		model.addAttribute("page", page);
		
		UserUtils.putCache("askRoom", askRoom);
		return "modules/business/askRoomList";
	}

	@RequiresPermissions("business:askRoom:view")
	@RequestMapping(value = "form")
	public String form(AskRoom askRoom, Model model) {
		model.addAttribute("askRoom", askRoom);
		return "modules/business/askRoomForm";
	}

	@RequiresPermissions("business:askRoom:edit")
	@RequestMapping(value = "save")
	public String save(AskRoom askRoom, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, askRoom)){
			return form(askRoom, model);
		}
		askRoomService.save(askRoom);
		addMessage(redirectAttributes, "保存申请房屋成功");
		
		redirectAttributes.addFlashAttribute("askRoom", UserUtils.getCache("askRoom"));
		return "redirect:"+Global.getAdminPath()+"/business/askRoom/?repage";
	}

	@RequiresPermissions("business:askRoom:edit")
	@RequestMapping(value = "confirm")
	public String confirm(AskRoom askRoom, Model model, RedirectAttributes redirectAttributes) {
		askRoom.setStatus(AskRoom.STATUS_PASS);
		Room room = roomDao.get(askRoom.getRoomId());
		room.setHouseHolderId(askRoom.getCustomerId());
		room.setUsingCustomerId(askRoom.getCustomerId());
		roomDao.update(room);
		askRoomService.save(askRoom);
		addMessage(redirectAttributes, "保存申请房屋成功");

		redirectAttributes.addFlashAttribute("askRoom", UserUtils.getCache("askRoom"));
		return "redirect:" + Global.getAdminPath() + "/business/askRoom/?repage";

	}

	@RequiresPermissions("business:askRoom:edit")
	@RequestMapping(value = "delete")
	public String delete(AskRoom askRoom, RedirectAttributes redirectAttributes) {
		askRoomService.delete(askRoom);
		addMessage(redirectAttributes, "删除申请房屋成功");
		
		redirectAttributes.addFlashAttribute("askRoom", UserUtils.getCache("askRoom"));
		return "redirect:"+Global.getAdminPath()+"/business/askRoom/?repage";
	}

}